package com.jingsheng.jingshengcity.mvp.ui.fragment.tab;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.jingsheng.jingshengcity.R;
import com.jingsheng.jingshengcity.di.component.DaggerHomeFragmentComponent;
import com.jingsheng.jingshengcity.di.module.HomeFragmentModule;
import com.jingsheng.jingshengcity.mvp.contract.HomeFragmentContract;
import com.jingsheng.jingshengcity.mvp.presenter.HomeFragmentPresenter;
import com.jingsheng.jingshengcity.mvp.ui.activity.SearchActivity;
import com.jingsheng.jingshengcity.mvp.ui.adapter.HomeTabAdapter;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * Created by Administrator on 2017/5/5.
 */

public class HomeFragment extends BaseFragment<HomeFragmentPresenter> implements HomeFragmentContract.View, View.OnClickListener {

    @NonNull
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @NonNull
    @BindView(R.id.vp_fragment)
    ViewPager mViewPager;
    @NonNull
    @BindView(R.id.tv_search)
    TextView mTvSearch;

    private String mTabTitle[] = new String[]{"首页", "服饰箱包", "母婴玩具", "生活服务", "美妆护肤", "数码家电"};

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragmentComponent
                .builder()
                .appComponent(appComponent)
                .homeFragmentModule(new HomeFragmentModule(this))//请将HomeFragmentModule()第一个首字母改为小写
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void initData() {
        HomeTabAdapter adapter = new HomeTabAdapter(getChildFragmentManager(), mTabTitle);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(mTabTitle.length);
        mTabLayout.setupWithViewPager(mViewPager);

        initEvent();

    }

    private void initEvent() {
        mTvSearch.setOnClickListener(this);
    }

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        UiUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_search:
                toSearch();
                break;
        }
    }

    private void toSearch() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }
}